package com.UI
{
	import com.*;
	import com.Engine.*;
	import com.Packet.*;
	
	import flash.display.*;
	import flash.display.BitmapData;
	import flash.events.*;
	import flash.external.*;
	import flash.filters.*;
	import flash.geom.*;
	import flash.net.*;
	import flash.system.*;
	import flash.text.*;
	import flash.ui.*;
	import flash.utils.*;
	import flash.utils.ByteArray;
	import flash.utils.getQualifiedClassName;
	
	import org.aswing.*;
	import org.aswing.border.*;
	import org.aswing.border.EmptyBorder;
	import org.aswing.colorchooser.*;
	import org.aswing.dnd.*;
	import org.aswing.event.*;
	import org.aswing.ext.*;
	import org.aswing.geom.*;
	import org.aswing.graphics.*;
	import org.aswing.tree.AbstractLayoutCache;
	
	public class PanelItemCSV extends JPanel
	{
		public var icon_id:uint = 0;
		public var index:uint = 0;
		//public var userObject:Object = null;
		
		//members define
		public var panelImage:JPanel;
		public var labelCount:JLabel;
		private var _item:Object = null;
		public function PanelItemCSV()
		{
			//component creation
			setSize(new IntDimension(45, 45));
			//setPreferredSize(new IntDimension(45, 45));
			var layout0:EmptyLayout = new EmptyLayout();
			setLayout(layout0);
			
			panelImage = new JPanel();
			panelImage.setLocation(new IntPoint(2, 2));
			panelImage.setSize(new IntDimension(40, 40));
			
			labelCount = new JLabel();
			labelCount.setLocation(new IntPoint(2, 28));
			labelCount.setSize(new IntDimension(41, 14));
			
			//component layoution
			append(panelImage);
			append(labelCount);
			
			addEventListener(MouseEvent.ROLL_OVER, onRollOver);
			addEventListener(MouseEvent.ROLL_OUT, onRollOut);
			//addEventListener(MouseEvent.CLICK, onClick);
			//addEventListener(ClickCountEvent.CLICK_COUNT, onItemClickCount);  
			
			//if(img_background == null || img_background_selected == null)
			//{
			//	img_background = new AssetBackground(Client.getUIElement("img_item_bg") as DisplayObject);
			//	img_background_selected = new AssetBackground(Client.getUIElement("img_item_bg_selected") as DisplayObject);
			//}
			if(showBackground)
				setBackgroundDecorator(new AssetBackground(Client.getUIElement("img_item_bg") as DisplayObject));
			
			var filter:GlowFilter;
			var font:ASFont = new ASFont("宋体", 11, false, false, false, false);
			filter = new GlowFilter(0x000000, 1, 2, 2, 10);
			labelCount.setFont(font);
			labelCount.setForeground(ASColor.WHITE);
			labelCount.setTextFilters(new Array(filter));
			labelCount.setHorizontalAlignment(AsWingConstants.RIGHT);
			//setClipMasked(false);
			//bringToTop(panelImage);
			//panelImage.setClipMasked(false);
			
		}
		public function clear():void
		{
			setBackgroundDecorator(null);
			panelImage.setBackgroundDecorator(null);
		}

		private var filterArray:Array = new Array(new GlowFilter(0xFFFF00, 1, 2, 2, 5));
		private function onRollOver(event:MouseEvent):void
		{
			
			if(filters != filterArray)
				filters = filterArray;
			
			if(this.parent is JPanel)
				(this.parent as JPanel).bringToTop(this);
		}
		
		private function onRollOut(event:MouseEvent):void
		{
			filters = null;
		}
		
		public function enableUse():Boolean
		{
			var enableUse:Boolean = true;
			var objItemCSV:Object = _item;
			if(objItemCSV != null)
			{
				if(objItemCSV.require_level > 0)
				{
					if(CharInfo.m_nLevel < objItemCSV.require_level)
					{
						enableUse = false;
					}
				}			
				if(objItemCSV.require_gender == 0 || objItemCSV.require_gender == 1)
				{
					if(CharInfo.m_nGender != objItemCSV.require_gender)
					{
						enableUse = false;
					}
					
				}
			}

			return enableUse;
		}
		public function isEmpty():Boolean
		{
			if(_item == null)
				return true;
			if(_item.csv_id <= 0)
				return true;
			return false;
		}
		//private var filterArrayImage:Array = new Array(new GlowFilter(0xEA0000, 0.6, 34, 34, 2,1,true));
		private var filterArrayImage:Array = new Array(new GlowFilter(0xEA0000, 1, 2, 2, 10));
		private var _tooltip:ToolTipItemCSV = new ToolTipItemCSV();
		public function set item(val:Object):void
		{
			_item = val;
			_tooltip.setTargetComponent(null);
			//panelImage.setBackgroundDecorator(null);
			labelCount.setText("");
			panelImage.filters = null;
			if(_item == null)
			{
				icon_id = 0;
				panelImage.setBackgroundDecorator(null);
				return;
			}
			if(_item.csv_id <= 0)
			{
				icon_id = 0;
				panelImage.setBackgroundDecorator(null);
				return;
			}
			var objItemCSV:Object = _item;//Client.getInstance().GetItemCSV(_item.item_id);
			
			_tooltip.setItemCSV(objItemCSV.id);
			_tooltip.setTargetComponent(this);
			if(icon_id != objItemCSV.icon_id)
			{
				icon_id = objItemCSV.icon_id;
				var dis:DisplayObject = Client.getItemElement("img_item_"+ objItemCSV.icon_id) as DisplayObject;
				panelImage.setBackgroundDecorator(dis != null ? new AssetBackground(dis) : null);
			}
			//labelCount.setText(_item.count > 1 ? _item.count.toString() : "");
			panelImage.setSize(new IntDimension(this.width-4, this.height-4));
			panelImage.setPreferredSize(new IntDimension(this.width-4, this.height-4));
			panelImage.filters = enableUse() ? null : filterArrayImage;
			panelImage.pack();
			panelImage.paintImmediately();
			pack();
			this.paintImmediately();
		}
		public function get item():Object
		{
			return _item;
		}
		private var _showBackground:Boolean = true;
		public function set showBackground(show:Boolean):void
		{
			_showBackground = show;
			setBackgroundDecorator(null);
			setBackgroundDecorator(_showBackground ? new AssetBackground(Client.getUIElement("img_item_bg") as DisplayObject) : null);
		}
		public function get showBackground():Boolean
		{
			return _showBackground;
		}
		//public static function hideClick():void
		//{
		//	FrameClickItem.hide();
		//}
		
		private var _enableClick:Boolean = true;
		public function set enableClick(click:Boolean):void
		{
			_enableClick = click;
		}
		public function get enableClick():Boolean
		{
			return _enableClick;
		}
		
	}
}
